<template>
  <el-card>
    <el-row>
      <el-col :span="4">
        <el-button size="mini" type="primary" @click="dialogVisible = true">添加文本</el-button>
      </el-col>
    </el-row>

    <el-table :data="textList" highlight-current-row stripe v-loading="loading" element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading" element-loading-background="#fafafa" style="width: 80%; margin-top: 10px;">
      <el-table-column type="expand" label="详情">
        <template slot-scope="scope">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item :label="scope.row.typeDesc" v-for="item in scope.row.texts" label-width="150px">
              <span>{{item.value}}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column type="index" label="编号" align="center" width="60">
      </el-table-column>
      <el-table-column prop="typeDesc" label="类型" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="warning" size="mini" @click="updateTextForm(scope.row)">修改</el-button>
          <el-button type="danger" size="mini" @click="deleteText(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加文本" :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
      <el-form :model="form" label-width="100px" class="form">
        <el-form-item label="类型">
          <el-select v-model="form.type" placeholder="请选择文本类型">
            <el-option v-for="item in textTypes" :key="item.code" :label="item.msg" :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="文本内容" v-for="item in form.texts">
          <el-input v-model="item.value" placeholder="文本项" type="text" style="width: 70%;"></el-input>
          <el-button @click.prevent="removeText(item,'add')" type="danger" size="mini" style="margin-left: 15px;">删除
          </el-button>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addTextNum('add')" type="warning" size="mini">新增文本项</el-button>
        <el-button type="primary" size="mini" @click="addText('add')">保存</el-button>
      </span>
    </el-dialog>

    <el-dialog title="添加文本" :visible.sync="updateDialogVisible" width="40%" :before-close="handleClose">
      <el-form :model="updateForm" label-width="100px" class="form">
        <el-form-item label="类型">
          <el-select v-model="updateForm.type" placeholder="请选择文本类型" disabled>
            <el-option v-for="item in textTypes" :key="item.code" :label="item.msg" :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="文本内容" v-for="item in updateForm.texts">
          <el-input v-model="item.value" placeholder="文本项" type="text" style="width: 70%;"></el-input>
          <el-button @click.prevent="removeText(item,'update')" type="danger" size="mini" style="margin-left: 15px;">删除
          </el-button>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addTextNum('update')" type="warning" size="mini">新增文本项</el-button>
        <el-button type="primary" size="mini" @click="addText('save')">保存</el-button>
      </span>
    </el-dialog>

  </el-card>
</template>

<script>
  import {
    addText,
    listText,
    deleteText,
    getDic
  } from '../../apis/XcxApi'
  export default {
    name: 'CommonText',
    data() {
      return {
        dialogVisible: false,
        updateDialogVisible: false,
        form: {
          texts: [{
            value: ''
          }]
        },
        textList: [],
        textTypes: [],
        updateForm: {},
        loading: false
      }
    },
    created() {
      this.getTextType()
      this.listText()
    },
    methods: {
      addText(type) {
        addText(type === 'add' ? this.form : this.updateForm).then(res => {
          if (res.data) {
            this.$message.success('操作成功')
            this.dialogVisible = false
            this.updateDialogVisible = false
            this.form = {
              texts: [{
                value: ''
              }]
            }
            this.updateForm = {}
            this.listText()
          } else {
            this.$message.error('操作失败')
          }
        })
      },
      listText() {
        this.loading = true
        listText().then(res => {
          this.textList = res.data
          this.loading = false
        })
      },
      deleteText(id) {
        deleteText({
          id: id
        }).then(res => {
          if (res.data > 0) {
            this.$message.success('删除成功')
            this.listText()
          } else {
            this.$message.error('删除失败')
          }
        })
      },
      addTextNum(type) {
        if (type === 'add') {
          this.form.texts.push({
            value: ''
          })
        } else {
          this.updateForm.texts.push({
            value: ''
          })
        }

      },
      removeText(item, type) {
        if (type === 'add') {
          var index = this.form.texts.indexOf(item)
          if (index !== -1) {
            this.form.texts.splice(index, 1)
          }
        } else {
          var index = this.updateForm.texts.indexOf(item)
          if (index !== -1) {
            this.updateForm.texts.splice(index, 1)
          }
        }

      },
      updateTextForm(form) {
        this.updateForm = form
        this.updateDialogVisible = true
      },
      getTextType() {
        getDic({
          type: 'textType'
        }).then(res => {
          this.textTypes = res.data
        })
      },
      // 关闭对话框
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done()
          })
          .catch(_ => {})
      }
    }
  }
</script>

<style scoped>
  .demo-table-expand {
    font-size: 0;
  }

  .demo-table-expand>>>label {
    width: 90px;
    color: #99a9bf;
  }

  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 100%;
  }
</style>
